<?php

namespace app\admin\service;


use app\admin\model\BoxBanRule;
use app\admin\validate\BoxBanRuleValidate;

class BoxBanRuleService extends BaseService
{


    public $ban_rule_config = ['ban_member' => '封号', 'ban_comment' => '限制评论'];

    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxBanRule();
    }

    public function getList()
    {
        $param = request()->param();
        $map = [];
        if (isset($param['time'][0]) && !empty($param['time'][0])) {
            $map[] = ['create_time', '>=', strtotime($param['time'][0])];

        }
        if (isset($param['time'][1]) && !empty($param['time'][1])) {
            $map[] = ['create_time', '<=', strtotime($param['time'][1])];
        }
        $data = parent::getList($map); // TODO: Change the autogenerated stub
        if ($data['count'] > 0) {
            foreach ($data['data'] as $k => $item) {
                $rule = json_decode($item['rule'], true);
                $data['data'][$k]['rule']=$rule;
                $data['data'][$k]['rule_labels']=[];
                $data['data'][$k]['create_time']=date('Y-m-d H:i:s',$item['create_time']);
                if (!$rule) continue;
                if(isset($rule['ban_member'])){
                    $data['data'][$k]['ban_member'] =$rule['ban_member'];
                    $data['data'][$k]['is_ban_member'] = true;
                }

                if(isset($rule['ban_comment'])){
                    $data['data'][$k]['ban_comment'] = $rule['ban_comment'];
                    $data['data'][$k]['is_ban_comment'] = true;

                }
            }
        }
        return $data;

    }

    public function edit()
    {
        $param = request()->param();
        $validate = new BoxBanRuleValidate();
        if (!$validate->check($param)) {
            return message($validate->getError(), false, null, 1);
        }

        $data = [];
        $data['id'] = $param['id']??'';
        $data['title'] = $param['title'];
        $data['desc'] = $param['desc'];
        $data['sort'] = $param['sort'];
        $res = [];
        //更新规则
        if(isset($param['is_ban_member']) && $param['is_ban_member'] == true){
            $res['ban_member'] = $param['ban_member'];
        }

        if(isset($param['is_ban_comment']) && $param['is_ban_comment'] == true){
            $res['ban_comment'] = $param['ban_comment'] ;
        }
        $data['rule'] = json_encode($res);
        return parent::edit($data); // TODO: Change the autogenerated stub
    }
}